#include <Python.h>
#include "numpy/arrayobject.h"
#include "level_set.hpp"
#include <iostream>
 
using namespace std;
 
// example to use numpy object: http://blog.debao.me/2013/04/my-first-c-extension-to-numpy/
// write a c extension ot Numpy: http://folk.uio.no/hpl/scripting/doc/python/NumPy/Numeric/numpy-13.html
static PyObject *
level_set_wrapper(PyObject *self, PyObject *args)
{
    PyObject *I=NULL,*phi=NULL;
	float W,H,mu,timestep,lambda,alfa,epsilon,potential_function,iters;
    PyArrayObject *arr_I=NULL;
	PyArrayObject *arr_phi=NULL;
    
    if (!PyArg_ParseTuple(args, "OOfffffffff", &I,&phi,&W,&H,&mu,&timestep,&lambda,&alfa,&epsilon,&potential_function,&iters)) return NULL;
    
    arr_I = (PyArrayObject*)PyArray_FROM_OTF(I, NPY_FLOAT32, NPY_IN_ARRAY);
    if (arr_I == NULL) return NULL;
	
	arr_phi = (PyArrayObject*)PyArray_FROM_OTF(phi, NPY_FLOAT32, NPY_IN_ARRAY);
    if (arr_phi == NULL) return NULL;
    
	npy_intp * shape = PyArray_DIMS(arr_I);  // npy_intp array of length nd showing length in each dim.
	
	
	
	drlse_Evolution((float*)arr_I->data, (float*)arr_phi->data, int(W), int(H), mu, timestep, lambda, alfa,
                                 epsilon, (int)potential_function, (int)iters);
    cout<<1<<endl;
    
	
    // level_set2D.InitPhi(mask, image_width, image_height)
 
    // alfa = 0;
    // level_set2D.Evolution();
    // float* final_phi = level_set2D.GetPhi();
 
	// PyArrayObject * phi_np = (PyArrayObject*) PyArray_SimpleNewFromData(2, shape, NPY_FLOAT64, final_phi);
	Py_DECREF(arr_I);
	//Py_DECREF(arr_phi);
	return PyArray_Return(arr_phi);
 
    
}
 
static PyMethodDef Methods[] = {
    {"drlse",  level_set_wrapper, METH_VARARGS, "computing drlse by cpp"}, 
    {NULL, NULL, 0, NULL}
};